package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BusinessSeller;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.im.IMFactory;

import java.util.List;

public class SellerListAdapter<T> extends MyBaseAdapter<BusinessSeller> {

	public SellerListAdapter(Context ctx, List<BusinessSeller> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {
		
		ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context,R.layout.view_seller_list_item, null);
            holder = new ViewHolder();
            holder.titleText = (TextView) convertView.findViewById(R.id.textview_name);
            holder.addressText = (TextView) convertView.findViewById(R.id.textview_address);
            holder.zhengzaiText = (TextView) convertView.findViewById(R.id.textview_product);
            holder.yigongText = (TextView) convertView.findViewById(R.id.textview_yigonggei);
            holder.callPhoneImage = (ImageView) convertView.findViewById(R.id.imageview_call_phone);
            holder.callÇhatImage = (ImageView) convertView.findViewById(R.id.imageview_call_chat);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
		
        final BusinessSeller item = list.get(pos);
        
        holder.titleText.setText(item.getUnit());
        holder.addressText.setText(item.getAddress());
        holder.zhengzaiText.setText(item.getCatname());
        holder.yigongText.setText(item.getSupplymarket());
        holder.callPhoneImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DeviceHelper.callPhone(context, item.getMobile(), item.getKey());
			}
		});
        holder.callÇhatImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				IMFactory.openChat(context, item.getUserid());
			}
		});
      //禁止自己发布订单联系方式
  		if (AccountManager.getUserId().equals(item.getUserid())) {
  			holder.callPhoneImage.setVisibility(View.INVISIBLE);
  			holder.callÇhatImage.setVisibility(View.INVISIBLE);
  		}
		return convertView;
	}
	
	static class ViewHolder{
		TextView titleText;
		TextView addressText;
		TextView zhengzaiText;
		TextView yigongText;
		ImageView callÇhatImage;
		ImageView callPhoneImage;
	}
	
}
